﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RhythmComet.Services;
using RhythmComet.Utils;
using FMOD;

namespace RhythmComet.Gameplay
{
    public class FrameBurst : DrawableGameComponent
    {
        private RhythmCometGame game;
        private GraphicsDeviceManager graphics;
        private Camera camera;
#if !XBOX
        private Mp3Player player;
#endif
        private BasicEffect effect;
        private VertexPositionColor[] vertices;

        private float verticalLimit;
        private float horizontalLimit;
        private float offset;
        private int totalLines;
        private float frameBurst;

        public FrameBurst(RhythmCometGame game) : base(game)
        {
            this.game = game;
            this.graphics = game.Graphics;
#if !XBOX
            this.player = game.Player;
#endif
            this.camera = game.Camera;
        }

        public override void Initialize()
        {
            vertices = new VertexPositionColor[40];

            for (int j = 0; j < 40; j++)
                vertices[j].Color = Color.White;

            verticalLimit = 180.0f;
            horizontalLimit = 340.0f;
            offset = -2.5f;
            totalLines = 1;
            frameBurst = 0.0f;

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
#if !XBOX
            frameBurst = game.SpectrumAnalyzer.SmoothSpectrum[0];
#else
            frameBurst = frameBurst + (float) RandomGenerator.RandomBetween(-0.2f, 0.2f);
            if (frameBurst > 1.0f)
                frameBurst = 1.0f;
#endif

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            effect = new BasicEffect(GraphicsDevice, null);

            effect.Projection = camera.Projection;
            effect.View = camera.View;
            effect.World = Matrix.Identity;

            verticalLimit = 180.0f;
            horizontalLimit = 340.0f;
            offset = -2.5f;

            for (int i = 0; i < 8; i++)
            {
                int index = i * 5;
                vertices[index++].Position = new Vector3(-horizontalLimit, verticalLimit, 0.0f);
                vertices[index++].Position = new Vector3(horizontalLimit, verticalLimit, 0.0f);
                vertices[index++].Position = new Vector3(horizontalLimit, -verticalLimit, 0.0f);
                vertices[index++].Position = new Vector3(-horizontalLimit, -verticalLimit, 0.0f);
                vertices[index].Position = new Vector3(-horizontalLimit, verticalLimit, 0.0f);

                verticalLimit += offset;
                horizontalLimit += offset;
            }

            effect.VertexColorEnabled = true;

            totalLines = (int)(frameBurst * 11);

            if (totalLines < 1)
                totalLines = 1;
            else if (totalLines > 8)
                totalLines = 8;

            effect.Begin();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();
                graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip,
                    vertices, 0, 4);
                pass.End();
            }
            effect.End();

            for (int i = 1; i < totalLines; i++)
            {
                effect.Begin();
                int begin = i * 5;
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Begin();
                    graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, vertices, begin, 4);
                    pass.End();
                }
                effect.End();
            }

            base.Draw(gameTime);
        }
    }
}
